package com.ymatou.iapi.usersafe.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.usersafe.parameter.RegistersafeCountBean;
import com.ymatou.iapi.usersafe.parameter.RegistersafeVerifyBean;
import com.ymatou.iapi.usersafe.service.RegistersafeVerifyCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserSafe;
import com.ymttest.database.model.RiskLog;
import com.ymttest.database.model.RiskResult;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 注册风控数据校验 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_RegistersafeVerify {
	private static RegistersafeVerifyBean registersafeverifyBean;
	private static RegistersafeVerifyCall registersafeverifyCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("注册风控数据校验");
	}

	@Before
	public void caseUp() {
		registersafeverifyBean = new RegistersafeVerifyBean();
		registersafeverifyCall = new RegistersafeVerifyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(registersafeverifyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private RegistersafeVerifyBean initSafeVerifyBean(RegistersafeCountBean sc) {
		RegistersafeVerifyBean rb = new RegistersafeVerifyBean();
		rb.setAppId(sc.getAppId());
		rb.setClientIp(sc.getClientIp());
		rb.setDeviceId(sc.getDeviceId());
		rb.setLoginId(sc.getLoginId());
		return rb;
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_RegistersafeVerify_001() {
		Logger.start(true, "注册风控数据校验成功,未风控");
		try {
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			JUserSafe.RegistersafeCount(sc);
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
			Logger.verifyEquals("null", result.get("message").toString(), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_RegistersafeVerify_002() {
		Logger.start(true, "不输入loginid,失败:20180312风控不关注loginid不校验by戈建华");
		try {
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			JUserSafe.RegistersafeCount(sc);
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyBean.setLoginId(null);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
//			Logger.verifyEquals(false, registersafeverifyCall.getJsonObject().get("Success").getAsBoolean(), "验证Success");
//			Logger.verifyEquals(100, registersafeverifyCall.getJsonObject().get("ErrorCode").getAsInt(), "验证ErrorCode");
//			Logger.verifyIsContains("not empty", registersafeverifyCall.getJsonObject().get("LastErrorMessage").toString(), "验证LastErrorMessage");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_RegistersafeVerify_003() {
		Logger.start(true, "只输入loginid，成功");
		try {
			registersafeverifyBean.setLoginId("test");
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
			Logger.verifyEquals("null", result.get("message").toString(), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_004() {
		Logger.start(true, "ip风控不通过校验risk_strategy的status为2风控,实际返回风控");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(2, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(1, "device");
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP_device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			for (int i = 0; i < 6; i++) {
				JUserSafe.RegistersafeCount(sc);
			}
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(false, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(101, result.get("code").getAsInt(), "验证code");
			Logger.verifyIsContains("IP风控", result.get("message").toString(), "验证message");
			
			List<RiskLog> rl = new UserSafeWapper().selectRiskLogByLoginId(sc.getLoginId());
			Logger.verifyEquals(1, rl.size(), "验证RiskLog有记录");
			Logger.comment("RiskLog:"+rl.toString());
			Logger.verifyEquals(true,rl.get(0).getStrategy_title().contains("IP风控"),"验证风控规则");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_005() {
		Logger.start(true, "deviceid风控不通过校验risk_strategy的status为2风控,实际返回风控");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(2, "device");
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP_device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			for (int i = 0; i < 6; i++) {
				JUserSafe.RegistersafeCount(sc);
			}
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(false, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(101, result.get("code").getAsInt(), "验证code");
			Logger.verifyIsContains("Device风控", result.get("message").toString(), "验证message");
			
			List<RiskLog> rl = new UserSafeWapper().selectRiskLogByLoginId(sc.getLoginId());
			Logger.verifyEquals(1, rl.size(), "验证RiskLog有记录");
			Logger.verifyEquals(true,rl.get(0).getStrategy_title().equals("Device风控"),"验证风控规则");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_006() {
		Logger.start(true, "ip加deviceid风控不通过校验risk_strategy的status为2风控,实际返回风控");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(1, "device");
			new UserSafeWapper().updateRiskStrategyStatus(2, "IP_device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			for (int i = 0; i < 6; i++) {
				JUserSafe.RegistersafeCount(sc);
			}
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(false, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(101, result.get("code").getAsInt(), "验证code");
			Logger.verifyIsContains("ip&Device风控", result.get("message").toString(), "验证message");

			List<RiskLog> rl = new UserSafeWapper().selectRiskLogByLoginId(sc.getLoginId());
			Logger.verifyEquals(1, rl.size(), "验证RiskLog有记录");
			Logger.verifyEquals(true,rl.get(0).getStrategy_title().equals("ip&Device风控"),"验证风控规则");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_007() {
		Logger.start(true, "注册风控数据校验成功deviceid校验成功");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(2, "device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			sc.setClientIp(null);
			JUserSafe.RegistersafeCount(sc);
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
			Logger.verifyEquals("null", result.get("message").toString(), "验证message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_008() {
		Logger.start(true, "注册风控数据校验成功deviceid加ip校验成功");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(2, "IP_device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			sc.setClientIp(null);
			JUserSafe.RegistersafeCount(sc);
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
			Logger.verifyEquals("null", result.get("message").toString(), "验证message");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_009() {
		Logger.start(true, "ip风控不通过校验risk_strategy的status为1监控,实际返回不风控");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(1, "device");
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP_device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			sc.setDeviceId(null);
			for (int i = 0; i < 6; i++) {
				JUserSafe.RegistersafeCount(sc);
			}
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
			Logger.verifyEquals("null", result.get("message").toString(), "验证message");
			List<RiskLog> rl = new UserSafeWapper().selectRiskLogByLoginId(sc.getLoginId());
			Logger.verifyEquals(true, rl.size()>0, "验证RiskLog有记录");
			RiskLog riskLog=rl.get(0);
			Logger.verifyEquals(sc.getClientIp(), riskLog.getClient_ip(), "验证clinetip");
			Logger.verifyEquals("1", riskLog.getStatus().toString(), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_010() {
		Logger.start(true, "deviceid风控不通过校验risk_strategy的status为1监控,实际返回不风控");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(1, "device");
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP_device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			sc.setClientIp(null);
			for (int i = 0; i < 6; i++) {
				JUserSafe.RegistersafeCount(sc);
			}
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
			Logger.verifyEquals("null", result.get("message").toString(), "验证message");
			List<RiskLog> rl = new UserSafeWapper().selectRiskLogByLoginId(sc.getLoginId());
			Logger.verifyEquals(true, rl.size()>0, "验证RiskLog有记录");
			RiskLog riskLog=rl.get(0);
			Logger.verifyEquals(sc.getDeviceId(), riskLog.getDevice_id(),"验证DeviceId");
			Logger.verifyEquals("1", riskLog.getStatus().toString(), "验证Status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_011() {
		Logger.start(true, "ip加deviceid风控不通过校验risk_strategy的status为1监控,实际返回不风控");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(1, "device");
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP_device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			for (int i = 0; i < 6; i++) {
				JUserSafe.RegistersafeCount(sc);
			}
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
			Logger.verifyEquals("null", result.get("message").toString(), "验证message");
			List<RiskLog> rl = new UserSafeWapper().selectRiskLogByLoginId(sc.getLoginId());
			Logger.verifyEquals(true, rl.size()>0, "验证RiskLog有记录");
			RiskLog riskLog=rl.get(0);
			Logger.verifyEquals(sc.getDeviceId(), riskLog.getDevice_id(),"验证DeviceId");
			Logger.verifyEquals(sc.getClientIp(), riskLog.getClient_ip(),"验证Client_ip");
			Logger.verifyEquals("1", riskLog.getStatus().toString(), "验证Status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_012() {
		Logger.start(true, "ip风控不通过校验risk_strategy的status为0关闭,实际返回不风控");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(0, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(1, "device");
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP_device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			sc.setDeviceId(null);
			for (int i = 0; i < 6; i++) {
				JUserSafe.RegistersafeCount(sc);
			}
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
			Logger.verifyEquals("null", result.get("message").toString(), "验证message");
			List<RiskLog> rl = new UserSafeWapper().selectRiskLogByLoginId(sc.getLoginId());
			
			//现在会做数据收集
			Logger.debug(FormatUtil.beanToGSONString(rl));
			Logger.verifyEquals(true, rl==null, "验证RiskLog无记录");
			
			List<RiskResult> rrl = new UserSafeWapper().selectRiskResult(sc.getClientIp());
			Logger.debug(FormatUtil.beanToGSONString(rrl));
			Logger.verifyEquals(true, rrl==null, "验证RiskResult无记录");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_013() {
		Logger.start(true, "deviceid风控不通过校验risk_strategy的status为为0关闭,实际返回不风控");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(0, "device");
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP_device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			sc.setClientIp(null);
			for (int i = 0; i < 6; i++) {
				JUserSafe.RegistersafeCount(sc);
			}
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
			Logger.verifyEquals("null", result.get("message").toString(), "验证message");
			List<RiskLog> rl = new UserSafeWapper().selectRiskLogByLoginId(sc.getLoginId());
			Logger.verifyEquals(true, rl==null, "验证RiskLog无记录");
			
			List<RiskResult> rrl = new UserSafeWapper().selectRiskResult(sc.getDeviceId());
			Logger.verifyEquals(true, rrl==null, "验证RiskResult无记录");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_RegistersafeVerify_014() {
		Logger.start(true, "ip加deviceid风控不通过校验risk_strategy的status为为0关闭,实际返回不风控");
		try {
			new UserSafeWapper().updateRiskStrategyStatus(1, "IP");
			new UserSafeWapper().updateRiskStrategyStatus(1, "device");
			new UserSafeWapper().updateRiskStrategyStatus(0, "IP_device");
			RegistersafeCountBean sc = JUserSafe.initSafeCountBean();
			for (int i = 0; i < 6; i++) {
				JUserSafe.RegistersafeCount(sc);
			}
			registersafeverifyBean = initSafeVerifyBean(sc);
			registersafeverifyCall.setData(registersafeverifyBean);
			registersafeverifyCall.callService();
			JsonObject result = registersafeverifyCall.getJsonObject().get("Result").getAsJsonObject();
			Logger.verifyEquals(true, result.get("success").getAsBoolean(), "验证success");
			Logger.verifyEquals(0, result.get("code").getAsInt(), "验证code");
			Logger.verifyEquals("null", result.get("message").toString(), "验证message");
			List<RiskLog> rl = new UserSafeWapper().selectRiskLogByLoginId(sc.getLoginId());
			
			//现在会做数据收集
			Logger.verifyEquals(1, rl.size(), "验证RiskLog有记录");
			Logger.verifyEquals(false,rl.get(0).getStrategy_title().equals("ip&Device风控"),"验证风控规则非ip&Device风控");
			
			List<RiskResult> rrl = new UserSafeWapper().selectRiskResult(sc.getClientIp()+"_"+sc.getDeviceId());
			Logger.verifyEquals(true, rrl==null, "验证RiskResult无记录");
			Logger.debug(FormatUtil.beanToGSONString(rrl));
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}